<?php



class Pagination{


    private static $tri;
    private static $sens;
    private static $noPage;
    private static $nbEnTable;
    private static $nbEnBase;


	public static function getList($class){
        //on aura besoin du tri sélectionné
        self::$tri=Form::getParam('tri');
        self::$sens=Form::getParam('sens');
        
        //on récupère le numéro de la page actuelle
        self::$noPage=Form::getParam('page');
        if(self::$noPage==null) self::$noPage=1;
        
        //on cherche les limites en fonction de la page à afficher
		self::$nbEnTable=Properties::getValue('nbElementsParPage');
        $limitFrom=(self::$noPage-1)*self::$nbEnTable;
        
        //le count en base
        self::$nbEnBase=$class::countAll();
        
        //et on charge la liste
        $tri='';
        if(self::$tri!=null){
            $tri='ORDER BY '.self::$tri;
            if(self::$sens!=null){
                $tri.=' DESC';
            }
        }
		return $class::loadGroup($limitFrom, self::$nbEnTable, $tri);
	}
    
    
	public static function getNavigation($class){
        $navig='';
        if(self::$nbEnBase>self::$nbEnTable){
            $navig='<div class="navigation"><span>Aller à la page :</span>';
            for($no=1;$no<(1+self::$nbEnBase/self::$nbEnTable);$no++){
                if($no==self::$noPage){
                    $navig.=' <span>'.$no.'</span>';
                }
                else{
                    $page=$no;
                    if(self::$tri){
                        $page.='&tri='.self::$tri;
                        if(self::$sens!=null){
                            $page.='&sens='.self::$sens;
                        }
                    }
                    $navig.=" <a href='?show=$class&page=$page'>$no</a>";
                }
            }
            $navig.='</div>';
        }
		return $navig;
	}
    
    
    public static function getSortableHeader($nom,$label){
        $page=self::$noPage;
        $ref=strtolower($nom);
        $ref.=self::$tri==$ref&&!isset($_GET['sens'])?'&sens=desc':'';
        
        $header='<th>';
        $header.="<a href='?tri=$ref'>$label</a>";
        if(self::$tri==strtolower($nom)){
            $lien=isset($_GET['sens'])?'ASC':'DESC';
            $header.=" <img src='static/img/icone_$lien.gif'/>";
        }
        $header.='</a></th>';
        return $header;
    }

	
}


?>